// 原始sql
const { QueryTypes, Op, literal, where } = require("sequelize");
const seq = require("../db/seq");
const moment = require("moment");
const chatGroup = require("../model/chatGroup.model")
const ChatNews = require("../model/chatNews.model")
class ChatNewsService {
    async addChatNewsService(data) {
        let requireObj = {
            ...data,
            createTime:moment().valueOf(),
        }
        const res = await ChatNews.create(requireObj);   
        console.log("addchat",res)
        return res;
        
    }

    async queryChatNewsServcie(data) {
        const res = await ChatNews.findAll({where:data});
        for (let i = 0; i < res.length; i++) {
            const id = res[i]["dataValues"].userId;
            console.log("article------@@",id);
            const data = await seq.query("select * from `bl-user` where id=?", {
              replacements: [id],
              type: QueryTypes.SELECT,
            });
            res[i]["dataValues"].authorInfo = data && data[0];
        }
        return res;
    }


    async queryGroupService() {
        const res = await seq.query("select * from `bl-chatgroup`",{
            type:QueryTypes.SELECT,
        })
        return res;
    }
    async queryGroupByIdService(id) {
        let requestObj = {
            id,
        }
        const res = await chatGroup.findOne({where:requestObj})
        console.log(res,"是否存在");
        if(res) {
            return true;
        }else {
            return false;
        }
    }

    async addGroupByIdService(data) {
        const res = await chatGroup.create(data);
        return res;
    }
}


module.exports = new ChatNewsService();
